<template>
  <div>
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <span>销售绩效</span>
      </div>
      <div class="searchAdd">
        <el-button class="addBtn" type="primary" @click="ShowAddDialog">
          添加
        </el-button>
        <el-col :span="15" style="width: 400px">
          <el-select
            v-model="Page.name"
            placeholder="请选择员工"
            @change="searchByName()"
          >
            <el-option
              v-for="item in allUser"
              :key="item.empLoginid"
              :label="item.empName"
              :value="item.empName"
            >
            </el-option>
          </el-select>
          <el-button
            type="info"
            style="margin-left: 20px"
            @click="searchAllInfo()"
          >
            查询所有数据
          </el-button>
        </el-col>
      </div>
      <div class="InfoTable">
        <el-table
          :data="tableData"
          v-loading="loading"
          style="width: 100%"
          max-height="400px"
          stripe
        >
          <el-table-column type="expand">
            <template slot-scope="props">
              <el-form
                label-position="left"
                inline
                class="demo-table-expand"
                style="padding-left: 100px"
              >
                <el-form-item label="姓名：">
                  <span>{{ props.row.empName }}</span>
                </el-form-item>
                <el-form-item label="部门：">
                  <span>{{ props.row.deptName }}</span>
                </el-form-item>
                <el-form-item label="绩效编码：">
                  <span>{{ props.row.saleperformCode }}</span>
                </el-form-item>
                <el-form-item label="绩效等级：">
                  <span>{{ props.row.performLevel }}</span>
                </el-form-item>
                <el-form-item label="生成时间：">
                  <span>{{ props.row.updateTime }}</span>
                </el-form-item>
                <el-form-item label="最后操作时间：">
                  <span>{{ props.row.updateTime }}</span>
                </el-form-item>
                <el-form-item label="操作人：">
                  <span>{{ props.row.operator }}</span>
                </el-form-item>
                <el-form-item label="备注：" style="width: 60% !important">
                  <span>{{ props.row.des }}</span>
                </el-form-item>
              </el-form>
            </template>
          </el-table-column>
          <el-table-column fixed prop="saleperformId" label="ID" width="100">
          </el-table-column>
          <el-table-column fixed prop="empName" label="姓名" width="120">
          </el-table-column>
          <el-table-column prop="deptName" label="部门" width="150">
          </el-table-column>
          <el-table-column prop="performLevel" label="绩效等级" width="120">
          </el-table-column>
          <el-table-column prop="updateTime" label="最后操作时间" width="210">
          </el-table-column>
          <el-table-column prop="operator" label="操作人" >
          </el-table-column>
          <el-table-column fixed="right" label="操作" width="180">
            <template slot-scope="scope">
              <el-button
                @click.native.prevent="changeRow(scope.row)"
                type="text"
                size="small"
              >
                修改
              </el-button>
              <el-button
                @click.native.prevent="deleteRow(scope.row)"
                type="text"
                size="small"
              >
                删除
              </el-button>
            </template>
          </el-table-column>
        </el-table>
        <el-pagination
          :page-size="Page.size"
          :pager-count="7"
          :current-page="Page.current"
          layout="prev, pager, next"
          :page-count="Page.pageCount"
          @current-change="handleCurrentChange"
          ref="pageCurrent"
        >
        </el-pagination>
      </div>
      <!-- 添加对话框 -->
      <el-dialog
        title="添加销售绩效"
        :visible.sync="AddDialogFormVisible"
        width="30%"
        @close="addDialogClosed"
      >
        <el-form
          ref="addDialogRef"
          label-width="65px"
          :model="addItem"
          :rules="addRules"
          style="padding-right: 50px"
        >
          <el-form-item
            label="绩效编码"
            label-width="120px"
            prop="saleperformCode"
          >
            <el-input v-model="addItem.saleperformCode"></el-input>
          </el-form-item>
          <el-form-item label="员工姓名" label-width="120px" prop="empLoginid">
            <el-select v-model="addItem.empLoginid" placeholder="请选择">
              <el-option
                v-for="item in allUser"
                :key="item.empLoginid"
                :label="item.empName"
                :value="item.empLoginid"
              >
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="所属部门" label-width="120px" prop="deptName">
            <el-select v-model="addItem.deptName" placeholder="请选择">
              <el-option
                v-for="item in allDept"
                :key="item.deptId"
                :label="item.deptName"
                :value="item.deptName"
              >
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item
            label="绩效等级"
            label-width="120px"
            prop="performLevel"
          >
            <el-input v-model.number="addItem.performLevel"></el-input>
          </el-form-item>
          <el-form-item label="备注信息" label-width="120px" prop="des">
            <el-input
              type="textarea"
              :rows="4"
              v-model="addItem.des"
            ></el-input>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="primary" @click="addPerfData">添加</el-button>
          <el-button @click="AddDialogFormVisible = false">关闭</el-button>
        </div>
      </el-dialog>
      <!-- 修改对话框 -->
      <el-dialog
        title="修改销售绩效信息"
        :visible.sync="ChangeDialogFormVisible"
        width="30%"
      >
        <el-form :model="itemDate" style="padding-right: 50px">
          <el-form-item label="绩效编码" label-width="120px">
            <el-input v-model="itemDate.saleperformCode"></el-input>
          </el-form-item>
          <el-form-item label="员工姓名" label-width="120px">
            <el-select v-model="itemDate.empid" placeholder="请选择">
              <el-option
                v-for="item in allUser"
                :key="item.empLoginid"
                :label="item.empName"
                :value="item.empLoginid"
              >
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="所属部门" label-width="120px">
            <el-select v-model="itemDate.deptName" placeholder="请选择">
              <el-option
                v-for="item in allDept"
                :key="item.deptId"
                :label="item.deptName"
                :value="item.deptName"
              >
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="绩效等级" label-width="120px">
            <el-input v-model="itemDate.performLevel"></el-input>
          </el-form-item>
          <el-form-item label="备注信息" label-width="120px">
            <el-input
              type="textarea"
              :rows="4"
              v-model="itemDate.des"
            ></el-input>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="changeSubmit">修改</el-button>
          <el-button @click="ChangeDialogFormVisible = false">关闭</el-button>
        </div>
      </el-dialog>
    </el-card>
  </div>
</template>

<script>
import pubsub from "pubsub-js";

export default {
  name: "perf",
  data() {
    return {
      // 加载状态
      loading: "",
      // 当前登录员工
      loginUserName: "",
      // 页面
      Page: {
        name: "",
        current: 1,
        size: 6,
        pageCount: 1,
      },
      // 表格数据
      tableData: [],
      // 添加预测窗口
      AddDialogFormVisible: false,
      // 修改窗口控制器
      ChangeDialogFormVisible: false,
      // 添加绩效
      addItem: {
        empLoginid: "",
        deptName: "",
        empName: "",
        operator: "",
        performLevel: "",
        saleperformCode: "",
        des: "",
      },
      // 所有员工
      allUser: [],
      // 所有部门
      allDept: [],
      // 单条数据
      itemDate: {},
      // 添加数据规则
      addRules: {
        saleperformCode: [
          { required: true, message: "请输入绩效编码", trigger: "blur" },
        ],
        empLoginid: [
          {
            required: true,
            message: "请选择员工",
            trigger: ["blur", "change"],
          },
        ],
        deptName: [
          {
            required: true,
            message: "请选择所属部门",
            trigger: ["blur", "change"],
          },
        ],
        performLevel: [
          { required: true, message: "请输入绩效等级", trigger: "blur" },
          {
            type: "number",
            message: "绩效等级必须为数字",
            trigger: "blur",
          },
        ],
        des: [{ required: true, message: "请输入备注信息", trigger: "blur" }],
      },
    };
  },
  mounted() {
    this.getAllUserInfo();
    this.getAllPerfInfo();
    pubsub.subscribe("loginUser", (_, data) => {
      this.loginUserName = data;
    });
  },
  methods: {
    // 通过员工姓名查找
    searchByName() {
      this.getAllPerfInfo();
    },
    // 获取所有绩效表格信息
    async getAllPerfInfo() {
      this.loading = true;
      if (this.Page.name == "") {
        const { data: res } = await this.$http.post("/saleperform/getAll", {
          current: this.Page.current,
          size: this.Page.size,
        });
        if (res.status != 200) {
          this.loading = false;
          return this.$message.error(res.msg);
        }
        this.tableData = res.data.content;
        this.Page.pageCount = res.data.pageCount;
        this.loading = false;
      } else {
        console.log(1);
        const { data: res } = await this.$http.post("/saleperform/getByName", {
          currentPage: this.Page.current,
          size: this.Page.size,
          name: this.Page.name,
        });
        if (res.status != 200) {
          this.loading = false;
          this.Page.name = "";
          return this.$message.error(res.msg);
        }
        this.tableData = res.data.content;
        this.Page.pageCount = res.data.pageCount;
        this.loading = false;
      }
    },
    // 页面改变
    handleCurrentChange(currentPage) {
      this.Page.current = currentPage;
      this.getAllPerfInfo();
    },
    // 获取所有员工信息
    async getAllUserInfo() {
      const { data: res } = await this.$http.post("/user/getAllUserInfo");
      if (res.status != 200)
        return this.$message.error("系统错误，请重新登录！");
      this.allUser = res.data;
    },
    // 按钮查询所有信息
    searchAllInfo() {
      this.Page.name = "";
      this.getAllPerfInfo();
    },
    //监听添加对话框关闭事件
    addDialogClosed() {
      this.resetAddForm();
    },
    // 重置
    resetAddForm() {
      this.$refs.addDialogRef.resetFields();
    },
    // 获取所有部门信息
    async getAllDeptInfo() {
      const { data: res } = await this.$http.post("/saleperform/getAllDept");
      if (res.status != 200)
        return this.$message.error("系统错误，请重新登录！");
      this.allDept = res.data;
    },
    // 添加绩效
    async addPerfData() {
      this.$refs.addDialogRef.validate(async (valid) => {
        if (this.getUserName(this.addItem.empLoginid)) {
          this.addItem.operator = this.loginUserName;
          console.log(this.addItem);
          if (!valid) return;
          const { data: res } = await this.$http.post("/saleperform/insert", {
            empid: this.addItem.empLoginid,
            empName: this.addItem.empName,
            deptName: this.addItem.deptName,
            performLevel: this.addItem.performLevel,
            operator: this.addItem.operator,
            saleperformCode: this.addItem.saleperformCode,
            des: this.addItem.des,
          });
          if (res.status != 200) {
            return this.$message.error(res.msg);
          }
          this.$message.success("添加成功");
          this.AddDialogFormVisible = false;
          this.getAllPerfInfo();
        } else return this.$message.error("系统错误，请重试");
      });
    },
    // 添加对话框
    ShowAddDialog() {
      this.getAllDeptInfo();
      this.operator = this.loginUserName;
      this.AddDialogFormVisible = true;
    },
    // 修改对话框
    changeRow(row) {
      this.getAllDeptInfo();
      this.itemDate = row;
      this.ChangeDialogFormVisible = true;
    },
    // 删除
    deleteRow(row) {
      this.$confirm("是否删除该计划?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(async () => {
          const { data: res } = await this.$http.post("/saleperform/delete", {
            id: row.saleperformId,
          });
          if (res.status != 200)
            return this.$notify.error({
              title: "失败",
              message: res.msg,
              duration: 2000,
            });
          this.getAllPerfInfo();
          this.$notify({
            title: "成功",
            message: res.msg,
            type: "success",
            duration: 2000,
          });
        })
        .catch(() => {
          this.$notify.info({
            title: "取消",
            message: "已取消删除",
            duration: 2000,
          });
        });
    },
    // 修改
    async changeSubmit() {
      const { data: res } = await this.$http.post("/saleperform/update", {
        saleperformId: this.itemDate.saleperformId,
        empid: this.itemDate.empid,
        empName: "",
        deptName: this.itemDate.deptName,
        performLevel: this.itemDate.performLevel,
        operator: this.loginUserName,
        saleperformCode: this.itemDate.saleperformCode,
        des: this.itemDate.des,
      });
      if (res.status != 200) {
        this.loading = false;
        return this.$message.error(res.msg);
      }
      this.$message.success(res.msg);
      this.ChangeDialogFormVisible = false;
      this.getAllPerfInfo();
    },
    // 获取用户名字
    async getUserName(data) {
      const { data: res } = await this.$http.post("/user/getname", {
        id: data,
      });
      if (res.status !== 200) {
        return false;
      }
      this.addItem.empName = res.data;
      return true;
    },
  },
};
</script>

<style scoped>
.clearfix {
  display: flex;
  justify-content: space-between;
  align-content: center;
  padding: 0 50px;
}
.searchAdd {
  width: 93%;
  margin: 0 auto;
  height: 40px;
  margin-bottom: 20px;
}

.InfoTable {
  width: 93%;
  margin: 0 auto;
}
.addBtn {
  float: left;
  width: 100px;
  height: 40px;
  margin-right: 20px;
}
</style>
